<?php

defined('_JEXEC') or die();

jimport('joomla.application.component.modellist');

class SEventModelEvents extends JModelList
{
	var $_total = null;
	
	var $_pagination = null;
	
	function __construct()
	{
		parent::__construct();
		
		// getting the current user
		$this->_user =& JFactory::getUser();
		
		if ($this->_user->guest) {
			$this->_userId = -1 ;
		} else {
			$this->_userId = $this->_user->id ;
		}
		
		$mainframe = JFactory::getApplication();
		// Get pagination request variables
		$limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		
		$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
		// In case limit has been changed, adjust it
		$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
		
		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
		
		$this->eventTypeTable =& JTable::getInstance('event_type', 'Table') ;
		$this->eventsTable =& JTable::getInstance('events', 'Table') ;
		$this->eventParticipantsTable =& JTable::getInstance('event_participants', 'Table') ;
	}
	
	function getEventTypes()
	{
		$query = 'SELECT types.event_type' .
				' FROM #__sevent_event_type AS types' ;
	
		//$this->event_types = $this->_getList($query) ;
		$this->_db->setQuery($query) ;
		$this->event_types = $this->_db->loadResultArray();
	
		return $this->event_types;
	}
	
	function getEvent($id)
	{
		$query = 'SELECT event.id, event.user_id, type.event_type, event.date_time, event.comment, event.user_notification'.
				' FROM #__sevent_events AS event'.
				' JOIN #__sevent_event_type AS type ON event.event_type = type.id'.
				' WHERE event.id =' . $id ;
		
		$this->_db->setQuery($query) ;
		return $this->_db->loadObject() ;
	}
	
	function getEvents()
	{	
		$query = 'SELECT *' .
				' FROM #__sevent_events  ORDER BY date_time DESC' ;
		
		return $this->_getList($query, $this->getState('limitstart'), $this->getState('limit')) ;
	}
	
	protected function getListQuery()
	{
		// Create a new query object.
		$query = 'SELECT *' .
				' FROM #__sevent_events  ORDER BY date_time DESC' ;
		return $query;
	}
	
	function getNumberOfEvents()
	{
		$query = 'SELECT * FROM #__sevent_events' ;
		$this->_total = $this->_getListCount($query);
		return $this->_total;
	}
	
	function getParticipants($event_id)
	{
		$query = 'SELECT participants.user_id' .
				' FROM #__sevent_event_participants AS participants'.
				' WHERE participants.event_id =' . (int)$event_id ;
		
		$this->_db->setQuery($query) ;
		return $this->_db->loadResultArray();
	}
	
	function storeEvent()
	{
		//$lineEvent['id'] = 0;
		$lineEvent['user_id'] = $this->_userId;
		
		$event_types = SEventModelEvents::getEventTypes();
		$eventType = JRequest::getVar('inputEventType');
		$key = array_search($eventType, $event_types);
		$lineEvent['event_type'] = $key + 1;
		$lineEvent['date_time'] = JRequest::getVar('publish_down') .' '. JRequest::getVar('inputTimeHours') .':'. JRequest::getVar('inputTimeMins').':00';
		$lineEvent['comment'] = JRequest::getVar('inputComment');
		$lineEvent['user_notification'] = JRequest::getBool('inputNotification');
		
		if (!$this->eventsTable->save($lineEvent))
		{
			return false;
		}
		
		return $this->eventsTable->id;
	}
	
	function changeComment($eventId, $newComment)
	{
		$query = 'UPDATE #__sevent_events'.
		' SET comment = '. $this->_db->Quote($newComment) .' WHERE id = '. $eventId ;
		
		$this->_db->setQuery($query);
		$this->_db->query();
	}
	
	function storeParticipant()
	{
		$eventid = JRequest::getVar('event_id');
		
		if(SEventModelEvents::checkParticipant($eventid, $this->_userId) == 0)
		{
			$lineParticipant['event_id'] = $eventid;
			$lineParticipant['user_id'] = $this->_userId;
			
			if (!$this->eventParticipantsTable->save($lineParticipant))
			{
				return false;
			}
		}
	}
	
	function checkParticipant($event_id, $user_id)
	{
		$query = 'SELECT COUNT(*)' .
				' FROM #__sevent_event_participants AS participants'.
				' WHERE participants.event_id =' . $event_id .' AND participants.user_id =' .$user_id ;
		
		$this->_db->setQuery($query);
		return  $this->_db->loadResult();
	}
	
	function isTheUserAutor($event_id, $userId)
	{
		$query = 'SELECT events.user_id' .
				' FROM #__sevent_events AS events'.
				' WHERE events.id =' . $event_id ;
		
		$this->_db->setQuery($query);
		$user_id_saved = $this->_db->loadResult();
		
		if($user_id_saved == $userId)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function getPagination()
	{
		// Load the content if it doesn't already exist
		if (empty($this->_pagination)) 
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination($this->getNumberOfEvents(), $this->getState('limitstart'), $this->getState('limit'));
		}
		return $this->_pagination;
	}
}